/**
 * 
 */
package com.csair.youth.dao.impl.hibernate3;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.csair.youth.dao.impl.hibernate3.base.CategoryDaoImplBase;
import com.csair.youth.database.Category;

/**
 * Impl:auto created by daobuilder1.1.1 
 * @version 1.0
 * @author daobuilder@csair.com
 */
public class CategoryDaoImpl extends CategoryDaoImplBase {
	private static final String DELETE_SUB_HQL = "delete from "+Category.REF+" a where a."+Category.PROP_ID+" in ";
	private static final String GET_ROOT_HQL = "from "+Category.REF+" a where a."+Category.PROP_PARENT_ID+" is null";
	private static final String GET_CATEGORY_BY_NAME_HQL = "from "+Category.REF+" a where a."+Category.PROP_NAME+"=:name";
	
	public void deleteCategoryAndSub(List<String> deleteIds){
		if(deleteIds != null && deleteIds.size() > 0){
			StringBuffer sb = new StringBuffer();
			int size = deleteIds.size();
			for(int i=size-1; i>=0; i--){
				sb.append("'"+deleteIds.get(i)+"'");
				if(i > 0){
					sb.append(",");
				}
			}
			String hql = DELETE_SUB_HQL + "(" + sb.toString() + ")";
			this.getGenericDao().execute(hql);
		}
	}
	
	public Category getRootCategory(){
		return this.findFirst(this.getGenericDao().find(GET_ROOT_HQL));
	}
	
	public Category getCategoryByName(String name){
		Map params = new HashMap();
		params.put("name", name);
		return this.findFirst(this.getGenericDao().find(GET_CATEGORY_BY_NAME_HQL, params, null));
	}
}
